View Javadoc

1   /*
2    * Created on 29-Dec-2004
3    */
4   package uk.ac.roe.antigen.utils;
5   
6   import java.io.IOException;
7   import java.util.Properties;
8   import java.util.logging.Logger;
9   
10  /***
11   * Make Properties globally available by wrapping a Properties with a static class
12   * @author jdt
13   */
14  public class Config {
15      /***
16       * Logger for this class
17       */
18      private static final Logger logger = Logger.getLogger(Config.class.getName());
19  
20  
21      private static Properties props = new Properties();
22      public static void load(String path) {
23          logger.fine("Loading properties off classpath from "+path);
24          try {
25  			props.load(Config.class.getResourceAsStream(path));
26  		} catch (Exception e) {
27  			logger.severe("Unable to load configuration file ...aborting.");
28              throw new RuntimeException("Unable to load properties",e);
29  		}
30      }
31      /***
32       * Returns null if the property is the empty string
33       * @param name property key
34       * @return the property with key name
35       */
36      public static String getProperty(String name) {
37          String property = props.getProperty(name);
38          if (property=="") property=null;
39  		return property;
40      }
41  	/***
42       * Returns the property from the props file, or the default if the property is null or the empty string
43  	 * @param name property key
44  	 * @param defolt default value
45  	 * @return property value
46  	 */
47  	public static String getProperty(String name, String defolt) {
48  		String value = getProperty(name);
49          return value !=null ? value : defolt;
50  	}
51  	/***
52       * Add in a property.  And why not.
53  	 * @param key key
54  	 * @param value value
55  	 */
56  	public static void setProperty(String key, String value) {
57  		props.setProperty(key, value);
58  	}
59  
60  }